package com.example.service.impl;

import com.example.dao.InitMapper;
import com.example.entity.*;
import com.example.service.InitService;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class InitServiceImpl implements InitService {
    private final InitMapper mapper;
    private final SqlSession sqlSession;

    public InitServiceImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
        this.mapper = sqlSession.getMapper(InitMapper.class);
    }
    @Override
    public void commit() {
        sqlSession.commit();
    }
    @Override
    public void rollback() {
        sqlSession.rollback();
    }

    @Override
    public boolean userAuth(String u_account, String u_password, HttpSession session) {
        User user = mapper.getUser(u_account, u_password);
        if (user == null) return false;
        session.setAttribute("user", user);
        return true;
    }
    @Override
    public User getUser(String account, String password) {
        return mapper.getUser(account, password);
    }
    @Override
    public String getHashedPasswordByAccount(String account) {
        return mapper.getHashedPasswordByAccount(account);
    }
    @Override
    public boolean managerAuth(String m_account, String m_password, HttpSession session) {
        Manager manager = mapper.getManager(m_account, m_password);
        if(manager == null) return false;
        session.setAttribute("manager", manager);
        return true;
    }
    @Override
    public Manager getManager(String m_account, String m_password) { return mapper.getManager(m_account, m_password); }
    @Override
    public List<String> getTop4ArrivalCities() {
        return mapper.getTop4ArrivalCities();
    }
    @Override
    public List<String> getTop4DepartureCities() {
        return mapper.getTop4DepartureCities();
    }
    @Override
    public Flight searchCheapestFlight(String departure, String destination, String departureDate){
        return mapper.searchCheapestFlight(departure, destination, departureDate);
    }

    @Override
    public List<Flight> getFlightList(String departure_date) {
        return mapper.getFlightList(departure_date);
    }
    @Override
    public FlightStatus getFlightStatusById(int f_id) {
        return mapper.getFlightStatusById(f_id);
    }
    @Override
    public int updateFlightStatus(String status, int f_id) {
        return mapper.updateFlightStatus(status, f_id);
    }
    @Override
    public List<Ticket> getTicketList() {
        return mapper.getTicketList();
    }
    @Override
    public int updateTicketStatus(int t_id) {
        return mapper.updateTicketStatus(t_id);
    }
    @Override
    public List<Order> getOrderList(String departure_date) {
        return mapper.getOrderList(departure_date);
    }
    @Override
    public Flight getFlightById(int f_id) {
        return mapper.getFlightById(f_id);
    }
    @Override
    public int updateOrderStatus(int o_id) {
        return mapper.updateOrderStatus(o_id);
    }
    @Override
    public List<Order> getUnpaidOrderList() {
        return mapper.getUnpaidOrderList();
    }
    @Override
    public List<Ticket> getTicketListByOrderId(int o_id) {
        return mapper.getTicketListByOrderId(o_id);
    }
    @Override
    public int addEconomySpace(int f_id) {
        return mapper.addEconomySpace(f_id);
    }
    @Override
    public int addBusinessSpace(int f_id) {
        return mapper.addBusinessSpace(f_id);
    }
    @Override
    public int addFirstSpace(int f_id) {
        return mapper.addFirstSpace(f_id);
    }
    @Override
    public int deleteTicket(int o_id) {
        return mapper.deleteTicket(o_id);
    }
    @Override
    public int deleteOrder(int o_id) {
        return mapper.deleteOrder(o_id);
    }
    @Override
    public int updateMileage(int passenger_id, double mileage) {
        return mapper.updateMileage(passenger_id, mileage);
    }
    @Override
    public Passenger getPassengerById(int passenger_id) {
        return mapper.getPassengerById(passenger_id);
    }
    @Override
    public int isUserExist(String ID_type, String ID_card) {
        return mapper.isUserExist(ID_type, ID_card);
    }
    @Override
    public int updateGrade(String grade, String ID_type, String ID_card) {
        return mapper.updateGrade(grade, ID_type, ID_card);
    }
}
